//(function () {
//    "use strict";
var gaOffer = angular.module('gaOffer', []);
/** @ngInject */
gaOffer.controller('offerListCtrl', offerListCtrl)
  .controller('offerAddCtrl', offerAddCtrl)
  .controller('offerEditCtrl', offerEditCtrl);

function offerListCtrl($scope, $http,$controller) {
  //	$http.get("http://localhost:8080/app/gaoffers/incents").success(function(data) {
  //		$scope.offers = data;
  //	});

  var vm = this;
  $scope.pageSize = 20;
  $scope.loadOfferList = loadOfferList;

  angular.extend(this, $controller('PaginationBaseController', {
    'vm': vm,
    'load': loadOfferList
  }));

  function loadOfferList() {
    $http({
      method: 'post',
      url: serverGwPath+'app/gaoffers/findByPage',
      data: {
        pageNum: 1,
        pageSize: 100
      }

    }).success(function (data) {

      $scope.offers = data.data;
      $scope.total = data.total;
      // console.log("6666"+JSON.stringify(data));
    })
  }

  loadOfferList();
  // console.log("6666"+$scope.offers);

}

//Add Controller
function offerAddCtrl($scope, $http, $location, offerService, $cookies) {

  var token = $cookies.get("token");
  if (token == null) {
    $location.path("/admin/login");
  }

  offerService.addOfferConfig().then(function (data) {
    $scope.osList = data.data.osList;
    $scope.adpositionList = data.data.adpositionList;
    $scope.incentList = data.data.incentList;
    $scope.incentTypeList = data.data.incentTypeList;
    $scope.countryList = data.data.countryList;

    chonsen_init("#putCountries");
    for (var i = 0; i < $scope.countryList.length; i++) {
      var obj = $scope.countryList[i];
      var html = "<option value='" + $scope.countryList[i].shortName + "'>" + $scope.countryList[i].fullName + "</option>";
      $("#putCountries").append(html);
    }
    $("#putCountries").trigger("chosen:updated")
  });


  //提交表单
  $scope.submitForm = function () {
    if ($scope.offer == null) {
      alert("请完善信息")
    } else {
      alert($scope.offer.countries);
      // offerService.addOffer($scope.offer);
    }
  };

  function chonsen_init(selector) {
    var config = {
      '.chosen-select': {},
      '.chosen-select-deselect': {allow_single_deselect: true},
      '.chosen-select-no-single': {disable_search_threshold: 10},
      '.chosen-select-no-results': {no_results_text: 'Oops, nothing found!'},
      '.chosen-select-rtl': {rtl: true},
      '.chosen-select-width': {width: '95%'}
    }
    $(selector).chosen(config[selector]);

    $(selector).chosen().change(function () {
      alert($scope.offer.countries);
    });

  }
}

function offerEditCtrl($scope, $http, $location, $routeParams, offerService) {


  offerService.addOfferConfig().then(function (data) {
    // console.log(data);
    $scope.osList = data.data.osList;
    $scope.adpositionList = data.data.adpositionList;
    $scope.incentList = data.data.incentList;
    $scope.incentTypeList = data.data.incentTypeList;
    $scope.countryList = data.data.countryList;

    var scope = $scope;
    offerService.getOfferById($routeParams.offerId).then(function (data) {
      $scope.offer = data;
      chonsen_init("#putCountries");
      for (var i = 0; i < $scope.countryList.length; i++) {
        // var obj = $scope.countryList[i];
        var html = "<option value='" + $scope.countryList[i].shortName + "'>" + $scope.countryList[i].fullName + "</option>";
        $("#putCountries").append(html);
      }
      // alert($scope.offer.countries);
      $scope.offer.countriesSelect = $scope.offer.countries;
      var countriesStr = $scope.offer.countries;
      var countriesArray = $scope.offer.countries.split(",");
      var countriesValue = [];
      for (var i = 0; i < countriesArray.length; i++) {
        countriesValue.push(countriesArray[i]);
      }
      $("#putCountries").val(countriesArray);
      $("#putCountries").trigger("chosen:updated");


      $("#putCountries").chosen().on("change", function (evt, params) {
        var countriesParams = JSON.stringify(params);

        if (params.deselected != null) {
          for (var i = 0; i < countriesArray.length; i++) {
            if (countriesArray[i] == params.deselected) {
              countriesArray.splice(i, 1);
              // console.log("移除" + params.deselected)

            }
          }
        } else if (params.selected != null) {
          // console.log("添加前" + countriesArray)
          if(countriesArray == ''){
            countriesArray = params.selected;
          }else{
            countriesArray.push(params.selected)
          }

          // console.log("添加" + params.selected)
          console.log("" + countriesArray)
        }
        $scope.offer.countries = countriesArray.toString();
        $("#putCountries").val(countriesArray);
        $("#putCountries").trigger("chosen:updated");
      });
    });

    //提交表单
    $scope.submitForm = function () {
      if ($scope.offer == null) {
        alert("请完善信息")
      } else {
        offerService.updateOffer($scope.offer);
      }
    };


  });

  function chonsen_init(selector) {
    var config = {
      '.chosen-select': {},
      '.chosen-select-deselect': {allow_single_deselect: true},
      '.chosen-select-no-single': {disable_search_threshold: 10},
      '.chosen-select-no-results': {no_results_text: 'Oops, nothing found!'},
      '.chosen-select-rtl': {rtl: true},
      '.chosen-select-width': {width: '95%'}
    }
    $(selector).chosen(config[selector]);


  }


}








